package com.table.generate;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.beyond.generate.entity.def.ColumnType;
import com.beyond.generate.service.SysGeneratorService;
import com.beyond.generate.service.impl.SysGeneratorServiceImpl;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 导出表字段
 *
 * @author zhangyan
 * @version $ Id: TableDoc.java, v 0.1 2020/6/4 15:30 zhangyan Exp $
 */
public class TableDocGenerator {

    public static void main(String[] args) throws Exception {
        Configuration configuration = new PropertiesConfiguration("generator.properties");
        List<ColumnType> columnTypes = getColumnTypes(configuration);
        List<Map<String, Object>> stringListMap = getStringListMap(columnTypes);
        writeExcel(configuration, stringListMap);
    }

    private static void writeExcel(Configuration configuration, List<Map<String, Object>> stringListMap) throws IOException {
        File file = new File(configuration.getString("targetProject") + "/xxx.xlsx");
        FileOutputStream outputStream = new FileOutputStream(file);
        Workbook workbook =
                ExcelExportUtil.exportExcel(stringListMap, ExcelType.XSSF);
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();

    }

    private static List<Map<String, Object>> getStringListMap(List<ColumnType> columnTypes) {
        Map<String, String> tableColumn = getTableColumn();
        List<Map<String, Object>> objectList = new ArrayList<>();
        Map<String, List<ColumnType>> groupBySex = columnTypes.stream().collect(Collectors.groupingBy(ColumnType::getTABLE_NAME));
        groupBySex.forEach((k, v) -> {
            Map<String, Object> sheetMap = new HashMap<>();
            ExportParams params = new ExportParams(tableColumn.get(k) + "\n" + k, tableColumn.get(k), ExcelType.XSSF);
            sheetMap.put("title", params);
            sheetMap.put("data", v);
            sheetMap.put("entity", ColumnType.class);
            objectList.add(sheetMap);
        });
        return objectList;
    }

    private static List<ColumnType> getColumnTypes(Configuration configuration) throws ConfigurationException {
        //读取配置文件
        SysGeneratorService service = new SysGeneratorServiceImpl();
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("databaseName", configuration.getString("databaseName"));
        return service.queryColumnType(paramsMap);
    }

    private static Map<String, String> getTableColumn() {
        //读取配置文件
        SysGeneratorService service = new SysGeneratorServiceImpl();
        return service.queryTableColumn();
    }
}
